//
// Created by yasin on 2024/9/6.
//

#ifndef WEBRTC_CLIENT_RTC_CLIENT_NATIVE_H_
#define WEBRTC_CLIENT_RTC_CLIENT_NATIVE_H_

#include <string>
#include <memory>
#include "api/video/video_render_interface.h"
#include "rtc_client/capturer/njs_camera_capture.h"
#include "rtc_client/capturer/njs_screen_capture.h"
#include "rtc_client/nativecpp/peer_connection_channel_native.h"
#include "rtc_client/nativecpp//peer_connection_dependency_factory_native.h"
#include "rtc_client/audio/ohos_audio_manager.h"
#include "rtc_client/capturer/njs_camera_capture.h"
#include "rtc_client/capturer/njs_screen_capture.h"
#include <stack>
#include "rtc_client/rtc_client_interface.h"
#include "rtc_client/rtc_wrapper.h"

namespace ohosrtc {

class SignalingClient;
class Signaling;
class PeerConnectionChannel;

class RTCClientNative : public RTCClientInterface {
 public:
   RTCClientNative();
   static RTCClientNative *Inst();
   //connect to calling room
   void Connect(const RoomConnectionParameters &params) override;
   //disconnect to calling room
   void Disconnect() override;
   // set remote render for video track to attach
	 void SetRemoteRender(std::shared_ptr<webrtc::VideoRenderInterface> render) override;
   // set remote render for camera track to attach
   void SetLocalRender(std::shared_ptr<webrtc::VideoRenderInterface> render) override;
	 void Hangup() override;
   void SetHangupCallback(std::function<void()> callback) override;
   // StartScreenCast by ohos
   void StartScreenCast() override;
   void StopScreenCast() override;
   // FlipCamera by ohos
	 void FlipCamera(bool is_front) override;
   void MuteMic() override;
   void UnMuteMic() override;
 private:
   // AddStream observer
	 void OnAddStream(MediaStreamPtr stream);
   // RemoveStream observer
	 void OnRemoveStream(MediaStreamPtr stream);
   // Open Screen Capture
   void StartScreenCapture();
   //observer on join room success  
   void OnSignalConnected();
   //observer on join room failed
   void OnSignalConnectFailed(const std::string &error);
   //use to make call
   void CreateP2pOffer();
   //handle to deal sdp offer and accept
   void HandleP2pOffer(webrtc::SessionDescriptionInterface *sdp);
   PeerConnectionChannelConfiguration GetPeerConnectionChannelConfiguration();
   //create open camera and bind sender 
   void CreateCameraSender(bool is_front);
   void CreateCameraSender_m(bool is_front);
   void FlipCamera_m(bool is_front);
   //create local stream for peer_connection and create peer_connection_channel
   void CreateStreamChannel();
   std::shared_ptr<PeerConnectionChannelNative> p2p_channel_;
   std::shared_ptr<PeerConnectionDependencyFactoryNative> pcd_factory_;
   std::shared_ptr<SignalingClient> signaling_client_;
   std::shared_ptr<webrtc::VideoRenderInterface> remote_render_;
   std::shared_ptr<webrtc::VideoRenderInterface> local_render_;
   std::shared_ptr<NJSCameraCapture> camera_capture_;
   std::function<void()> callback_;
   std::shared_ptr<NJSScreenCapture> screen_capture_;
   LocalStreamPtr screen_local_stream_;
   LocalStreamPtr capture_local_stream_;
   std::stack<MediaStreamPtr> remote_streams_;
   std::unique_ptr<rtc::Thread> media_thread_;
   std::shared_ptr<OHOSAudioManager> audio_manager_;
};

}

#endif //WEBRTC_CLIENT__RTC_CLIENT_H_
